package zabbix

import (
	"encoding/json"
	"errors"
)

func HttptestCreateData() (string,map[string]interface{}) {
	method := "httptest.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Homepage check",
        "hostid": "10085",
        "steps": [
            {
                "name": "Homepage",
                "url": "http://mycompany.com",
                "status_codes": "200",
                "no": 1
            },
            {
                "name": "Homepage / About",
                "url": "http://mycompany.com/about",
                "status_codes": "200",
                "no": 2
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HttptestDeleteData() (string,map[string]interface{}) {
	method := "httptest.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "2",
        "3"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HttptestGetData() (string,map[string]interface{}) {
	method := "httptest.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectSteps": "extend",
        "httptestids": "9"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HttptestUpdateData() (string,map[string]interface{}) {
	method := "httptest.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "httptestid": "5",
        "status": 0
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func HttptestCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HttptestCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HttptestDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HttptestDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HttptestGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HttptestGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HttptestUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HttptestUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}




